<template>
  <div class="mainpg">
    <div class="main_spac1" style="padding-bottom: 5px">
      <div class="main_white padding10">
        <div class="mainor_form small-top mg-b-16">
          <el-button
            type="primary"
            @click="(addedFormVisible = true), (type = 0)"
            >新增</el-button
          >
        </div>
        <div class="main_table" style="min-height: 670px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            :height="borderHeight"
            :header-cell-class-name="this.$verticaline"
            :header-cell-style="tableHeightStyle"
            @header-dragend="changeTableWidth"
          >
            <el-table-column
              label="名称"
              prop="title"
              :width="tableWidth[hash+'title']"
            />
            <el-table-column
              label="描述"
              prop="remark"
              :width="tableWidth[hash+'remark']"
            />
            <el-table-column
              label="操作"
              align="center"
              fixed="right"
              prop="operate"
              :width="tableWidth[hash+'operate'] || 200"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <el-link
                  type="primary"
                  @click="
                    (addedFormVisible = true),
                      (type = 1),
                      (id = scope.row.id),
                      edit_assig(scope.$index)
                  "
                  >编辑</el-link
                >
                <router-link :to="'/purchasing/group/maintain/' + scope.row.id">
                  <el-link type="success">维护</el-link>
                </router-link>
                <el-link
                  type="danger"
                  @click="(id = scope.row.id), delet(scope.$index)"
                  >删除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <MainPagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
    <el-dialog
      :title="type == 1 ? '编辑采购分组' : '新增采购分组'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="close"
      :visible.sync="addedFormVisible"
      class="main_dialog dialog400"
    >
      <el-form
        :inline="true"
        :model="info"
        :rules="rules"
        class="demo-form-inline"
        ref="userListFrom"
        label-width="65px"
      >
        <el-form-item label="名称：" prop="title">
          <el-input
            v-model.trim="info.title"
            placeholder="名称"
            clearable
            class="wid240"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述：" prop="remark">
          <el-input
            v-model.trim="info.remark"
            placeholder="描述"
            clearable
            class="wid240"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="(addedFormVisible = false), close()"
          >取 消</el-button
        >
        <el-button type="primary" @click="setupmine" v-repeatClick
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mixins } from '@/mixins/index.js'
import {
  purchaseGroup_index,
  purchaseGroup_add,
  purchaseGroup_edit,
  purchaseGroup_delete,
  purchaseGroup_confirmDelete,
} from "@/api/purchasing/purchasingGroup";
import rules from "@/rules/purchasing/purchasingGroup";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import MainPagination from '@/components/MainPagination'

export default {
  mixins: [mixins],
  components: { MainPagination },
  data() {
    return {
      // 增加和编辑
      info: {
        title: "",
        remark: "",
      },
      type: 0,
      id: 0,
      addedFormVisible: false,
      // 验证
      rules,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: {
          data: [],
        },
      },
      listLoading: false,
    };
  },
  methods: {
    // 关闭清空表单
    close() {
      this.info = {
        title: "",
        remark: "",
      };
      this.$refs["userListFrom"].clearValidate();
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.purchaseGroup_index();
    },
    // 删除
    delet(index) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.id }
      );
      this.$confirm("是否删除采购分组?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          purchaseGroup_delete(data).then((res) => {
            if (res.errcode == 0) {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.list.list.data.splice(index, 1);
            } else if (res.errcode == 40030) {
              this.$confirm("是否强制删除采购分组?", "提示", {
                confirmButtonText: "强制删除",
                cancelButtonText: "取消",
                type: "warning",
              })
                .then(() => {
                  purchaseGroup_confirmDelete(data).then((res) => {
                    if (res.errcode == 0) {
                      this.list.list.data.splice(index, 1);
                    }
                  });
                })
                .catch(() => {});
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
        .catch(() => {});
    },
    // 确认
    setupmine() {
      this.$refs["userListFrom"].validate((valid) => {
        if (valid) {
          this.type == 0 ? this.add() : this.edit();
        }
      });
    },
    // 编辑赋值
    edit_assig(index) {
      this.info = {
        title: this.list.list.data[index].title,
        remark: this.list.list.data[index].remark,
      };
    },
    // 新增保存
    add() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.info)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.info
      );
      purchaseGroup_add(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.current.page = 1;
          this.purchaseGroup_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 编辑保存
    edit() {
      let info = JSON.parse(JSON.stringify(this.info));
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, info, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        info,
        { id: this.id }
      );

      purchaseGroup_edit(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "编辑成功",
            type: "success",
          });
          this.purchaseGroup_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.purchaseGroup_index();
    },
    changePage(page) {
      this.current.page = page;
      this.purchaseGroup_index();
    },
    // 列表
    purchaseGroup_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.current)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.current
      );
      purchaseGroup_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
  },
  created() {
    // 列表
    this.getNumerical(160)
    this.purchaseGroup_index();
  },
};
</script>
<style lang="scss" scoped>
</style>
